﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using Storage;
using Storage.EntityModels;
using System.Data.Linq;

namespace Stats
{
    /// <summary>
    /// This class contains method related to distance computation. 
    /// </summary>
    public class AuxDistanceMethods
    {
        /// <summary>
        /// calculates the number of kilometers in a single stage
        /// </summary>
        /// <param name="stageID">the Id of the stage</param>
        /// <returns>the number of kilometers in the stageID stage</returns>
        public static double getStageKm(int stageID)
        {
            double stageKm = 0;

            try
            {
                TripLoaDataContext db = StorageManager.Instance.Entities;

                stageKm =
                    (from stageEntry in db.Stages
                     where stageEntry.Id == stageID
                     select stageEntry.Distance).First();
            }
            catch (InvalidOperationException) { stageKm = 0; }
            catch (NullReferenceException) { stageKm = 0; }
            catch (System.Data.SqlClient.SqlException) { stageKm = 0; }
            
            return stageKm;
        }

        /// <summary>
        /// calculates the number of kilometers in an entire trip
        /// </summary>
        /// <param name="tripID">the Id of the trip</param>
        /// <returns>the number of kilometers in the tripID trip</returns>
        public static double getTripKm(int tripID)
        {
            double tripTotalKm = 0;

            try
            {
                TripLoaDataContext db = StorageManager.Instance.Entities;

                var tripStageEntries =
                    from tripStageEntry in db.Stages
                    where tripStageEntry.TripId == tripID
                    select tripStageEntry;

                foreach (Stage stageEntry in tripStageEntries)
                {
                    tripTotalKm = tripTotalKm + AuxDistanceMethods.getStageKm(stageEntry.Id);
                }

            }
            catch (InvalidOperationException) { tripTotalKm = 0; }
            catch (NullReferenceException) { tripTotalKm = 0; }
            catch (System.Data.SqlClient.SqlException) { tripTotalKm = 0; }

            return tripTotalKm;

        }

        /// <summary>
        /// calculates the number of kilometers one user has done as a passenger
        /// </summary>
        /// <param name="userID">the user Id</param>
        /// <returns>the number of kilometers</returns>
        public static double getUserTotalKmAsPassenger(int userID)
        {
            double totalKmAsPassenger = 0;

            try
            {
                TripLoaDataContext db = StorageManager.Instance.Entities;

                var userStagesEntries =
                    from userStageEntry in db.Users_Stages
                    where userStageEntry.UserId == userID
                    select userStageEntry;

                foreach (var stageEntry in userStagesEntries)
                {
                    totalKmAsPassenger += getStageKm(stageEntry.StageId);
                }
            }
            catch (InvalidOperationException) { totalKmAsPassenger = 0; }
            catch (NullReferenceException) { totalKmAsPassenger = 0; }
            catch (System.Data.SqlClient.SqlException) { totalKmAsPassenger = 0; }

            return AuxMethods.TruncateTo3Decimals(totalKmAsPassenger);
        }

        /// <summary>
        /// calculates the number of kilometers done as driver
        /// </summary>
        /// <param name="userID">the user Id</param>
        /// <returns>the number of kilometers</returns>
        public static double getUserTotalKmAsDriver(int userID)
        {
            double totalKmAsDriver = 0;

            try
            {
                TripLoaDataContext db = StorageManager.Instance.Entities;

                var stageEntries =
                    from stageEntry in db.Stages
                    where stageEntry.DriverId == userID
                    select stageEntry;

                foreach (var stageEntry in stageEntries)
                {
                    totalKmAsDriver += getStageKm(stageEntry.Id);
                }

            }
            catch (InvalidOperationException) { totalKmAsDriver = 0; }
            catch (NullReferenceException) { totalKmAsDriver = 0; }
            catch (System.Data.SqlClient.SqlException) { totalKmAsDriver = 0; }

            return AuxMethods.TruncateTo3Decimals(totalKmAsDriver);
        }

       
    }
}
